package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.entity.Report;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetMealMapper setMealMapper;

    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO businessDataToday() {
        //获取当天的营业额
        LocalDate today = LocalDate.now();
        LocalDateTime beginTime = LocalDateTime.of(today, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.now();
        return this.businessData(beginTime,endTime);
    }

    /**
     * 根据时间范围查询运营数据
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime beginTime, LocalDateTime endTime) {
        Double turnover = orderMapper.getTurnover(beginTime, endTime, Orders.COMPLETED) == null ? 0.0 : orderMapper.getTurnover(beginTime, endTime, Orders.COMPLETED);

        //获取当天的有效订单数以及总的订单数
        Integer totalOrderCount = orderMapper.count(Report.builder().beginTime(beginTime).endTime(endTime).build());
        Integer validOrderCount = orderMapper.count(Report.builder().beginTime(beginTime).endTime(endTime).Status(Orders.COMPLETED).build());

        //获取订单完成率
        Double orderCompletionRate = totalOrderCount == null || totalOrderCount == 0 ? 0.0 : Double.valueOf(validOrderCount) / totalOrderCount;

        //获取平均客单价
        Double unitPrice = validOrderCount == null || validOrderCount == 0  ? 0.0 : turnover / validOrderCount;

        //获取新增用户数
        Integer newUsers = userMapper.count(Report.builder().beginTime(beginTime).endTime(endTime).build());

        //封装到营业数据中去并返回
        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrder() {
        //查询待接单数量
        Integer waitingOrders = orderMapper.countByStatus(Orders.TO_BE_CONFIRMED);

        //查询待派送数量
        Integer deliveredOrders = orderMapper.countByStatus(Orders.CONFIRMED);

        //查询已完成数量
        Integer completedOrders = orderMapper.countByStatus(Orders.COMPLETED);

        //查询已取消数量
        Integer cancelledOrders = orderMapper.countByStatus(Orders.CANCELLED);

        //查询全部订单
        Integer allOrders = orderMapper.count(new Report());

        //封装返回对象
        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE);
        Integer discontinued = dishMapper.countByStatus(StatusConstant.DISABLE);

        return new DishOverViewVO(sold,discontinued);
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetMeal() {
        Integer sold = setMealMapper.countByStatus(StatusConstant.ENABLE);
        Integer discontinued = setMealMapper.countByStatus(StatusConstant.DISABLE);

        return new SetmealOverViewVO(sold,discontinued);
    }
}
